package com.example.demo.service;

import com.example.demo.mapper.SchoolMapper;
import com.example.demo.pojo.School;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.List;
/**
 * @version 1.0
 * @Author liutao960916@outlook.com
 * @create 2021/6/17 14:25
 */
@Service
public class SchoolService {

    @Autowired
    SchoolMapper schoolMapper;
    @Autowired
    RedisTemplate redisTemplate;

    public List<School> findByName(String schoolName) {

        List<School> list = (List<School>)redisTemplate.opsForValue().get(schoolName);

        //如果没有取到，就从数据库从查询，然后存入缓存中
        if(list == null){
            list = schoolMapper.findSchoolByName(schoolName);
            redisTemplate.opsForValue().set(schoolName,list);
        }
        return list;
    }

    public List<School> findByProvince(String province) {

        List<School> list = (List<School>)redisTemplate.opsForValue().get(province);
        //如果没有取到，就从数据库从查询，然后存入缓存中
        if(list == null){
            list = schoolMapper.findSchoolByProvince(province);
            redisTemplate.opsForValue().set(province,list);
        }
        return list;
    }

    public List<School> findByType(String type) {

        List<School> list = (List<School>)redisTemplate.opsForValue().get(type);
        //如果没有取到，就从数据库从查询，然后存入缓存中
        if(list == null){
            list = schoolMapper.findSchoolByType(type);
            redisTemplate.opsForValue().set(type,list);
        }
        return list;
    }

    public List<School> ListSchool() {
        return schoolMapper.ListSchool();
    }
}

